package com.swgk.core.base;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleObserver;

import android.view.View;
import android.widget.RelativeLayout;

import com.swgk.core.BaseAppApplication;
import com.swgk.core.BaseAppHelper;
import com.swgk.core.R;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.base.net.NetBroadcastReceiver;
import com.swgk.core.util.LiuHaiPingUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * Description:
 * Author zhengkewen
 * Time 2017/12/11
 */
public abstract class BaseFragment extends Fragment implements View.OnClickListener {
    private static final int MIN_CLICK_DELAY_TIME = 2000;
    private static long lastClickTime;
    private NetBroadcastReceiver netBroadcastReceiver;
    protected int isNeedSetComponent;

    protected abstract boolean registerNet();

    protected abstract void connectNetWork();

    protected abstract void disconnectNetWork();

    protected abstract void setupFragmentComponent(AppComponent appComponent);

    public abstract void onMultiClick(View v);

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isNeedSetComponent != 1) {
            setupFragmentComponent(BaseAppHelper.get().getAppComponent());
        }
    }
    /**
     * 是否使用eventBus
     */
    public boolean isUseEventBus() {
        return false;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View liuHaiView = view.findViewById(R.id.liuhai_status_view);
        if (liuHaiView != null) {
            liuHaiView.getLayoutParams().height = LiuHaiPingUtils.getStatusBarHeight(getActivity());
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (registerNet()) {
            registerNetBroadcast();
        }
    }

    @Override
    public void onClick(View v) {
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            lastClickTime = curClickTime;
            onMultiClick(v);
        }
    }

    private void registerNetBroadcast() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netBroadcastReceiver = new NetBroadcastReceiver(status -> {
            if (status) {
                connectNetWork();
            } else {
                disconnectNetWork();
            }
        });
        //注册广播接收
        getActivity().registerReceiver(netBroadcastReceiver, filter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (registerNet()) {
            getActivity().unregisterReceiver(netBroadcastReceiver);
        }
    }
}
